/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package sgt.supermarket.controller.sales;

import java.io.IOException;
import java.io.PrintWriter;
import java.text.ParseException;
import java.util.Date;
import java.util.logging.Level;
import java.util.logging.Logger;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import sgt.supermarket.controller.XServlet;
import sgt.supermarket.entity.SaleProgram;
import sgt.supermarket.hibernatedao.HibernateDAOFactory;
import sgt.supermarket.util.Converter;
import sgt.supermarket.util.ServletExtensions;

/**
 *
 * @author Ms.MIAYU
 */
public class EditSaleProgram extends XServlet {

    /** 
     * Processes requests for both HTTP <code>GET</code> and <code>POST</code> methods.
     * @param request servlet request
     * @param response servlet response
     * @throws ServletException if a servlet-specific error occurs
     * @throws IOException if an I/O error occurs
     */
    protected void processRequest(HttpServletRequest request, HttpServletResponse response)
            throws ServletException, IOException, ParseException {

        HibernateDAOFactory factory = new HibernateDAOFactory();
        long saleProgramId = ServletExtensions.getLongParam(request, "id");
        SaleProgram saleProgram = saleProgramId != 0 ? factory.getSaleProgramDAO().getById(saleProgramId) : new SaleProgram();
        if (saleProgram.getAutoId() != 0) {
            request.setAttribute("title", "Edit Sale Program");
        } else {
            request.setAttribute("title", "Create new sale program");
        }

        if (request.getMethod().equalsIgnoreCase("POST")) {
            String startDate = request.getParameter("startDate") + "/" + request.getParameter("startDate-mm") + "/" + request.getParameter("startDate-dd");
            saleProgram.setStartDate(Converter.toDate(startDate, "yyyy/M/d"));
            String endDate = request.getParameter("endDate") + "/" + request.getParameter("endDate-mm") + "/" + request.getParameter("endDate-dd");
            saleProgram.setEndDate(Converter.toDate(endDate, "yyyy/M/d"));
            saleProgram.setDiscountRate(ServletExtensions.getDoubleParam(request, "discountRate"));
            saleProgram.setMinimumInvoiceTotal(ServletExtensions.getDoubleParam(request, "minimumInvoiceTotal"));
            factory.getSaleProgramDAO().save(saleProgram);
            response.sendRedirect("listSalePrograms");
            return;
        }
        request.setAttribute("saleProgram", saleProgram);
        request.getRequestDispatcher("/sales.editSaleProgram.tiles").forward(request, response);
    }

    // <editor-fold defaultstate="collapsed" desc="HttpServlet methods. Click on the + sign on the left to edit the code.">
    /** 
     * Handles the HTTP <code>GET</code> method.
     * @param request servlet request
     * @param response servlet response
     * @throws ServletException if a servlet-specific error occurs
     * @throws IOException if an I/O error occurs
     */
    @Override
    protected void doGet(HttpServletRequest request, HttpServletResponse response)
            throws ServletException, IOException {
        try {
            processRequest(request, response);
        } catch (ParseException ex) {
            Logger.getLogger(EditSaleProgram.class.getName()).log(Level.SEVERE, null, ex);
            throw new ServletException(ex);
        }
    }

    /** 
     * Handles the HTTP <code>POST</code> method.
     * @param request servlet request
     * @param response servlet response
     * @throws ServletException if a servlet-specific error occurs
     * @throws IOException if an I/O error occurs
     */
    @Override
    protected void doPost(HttpServletRequest request, HttpServletResponse response)
            throws ServletException, IOException {
        try {
            processRequest(request, response);
        } catch (ParseException ex) {
            Logger.getLogger(EditSaleProgram.class.getName()).log(Level.SEVERE, null, ex);
            throw new ServletException(ex);
        }
    }

    /** 
     * Returns a short description of the servlet.
     * @return a String containing servlet description
     */
    @Override
    public String getServletInfo() {
        return "Short description";
    }// </editor-fold>
}
